package com.fieldchain.ex.fa100.utils.chart.utils.render;

import android.util.SparseArray;

import com.fieldchain.ex.fa100.utils.chart.auxiliary.axis.MyXAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.LineData;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/13
 */
public class RenderUtils {
    private static int yCount = 4;

    public static void notifyXy(LineData lineData, List<List<String>> mList, MyXAxis xAxis, YAxis yAxis) {
        notify(mList, lineData.getYMin(), lineData.getYMax(), xAxis, yAxis);
    }

    public static void notifyXy(BarData barData, List<List<String>> mList, MyXAxis xAxis, YAxis yAxis) {
        notify(mList, barData.getYMin(), barData.getYMax(), xAxis, yAxis);
    }

    public static void notify(List<List<String>> mList, float minY, float maxY, MyXAxis xAxis, YAxis yAxis) {
        //X 轴
        SparseArray<String> xLabels = new SparseArray<>();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        if(mList.size()>3) {
            long t1 = Long.parseLong(mList.get(0).get(0));
            long t2 = Long.parseLong(mList.get(1).get(0));
            long t3 = Long.parseLong(mList.get(2).get(0));
            int day = 60 * 60 * 24*1000;
            if ((t2 - t1) > day && (t3 - t2) > day) {
                sdf = new SimpleDateFormat("MM-dd");
            } else {
                sdf = new SimpleDateFormat("HH:mm");
            }
        }
        for (int i = 0; i < mList.size(); i += 6) {
            String label = sdf.format(Long.parseLong(mList.get(i).get(0)));
            xLabels.put(i, label);
        }
        xAxis.setXLabels(xLabels);


        //Y 轴
//        SparseArray<String> yLabels = new SparseArray<>();
//        float dy = (maxY - minY) / yCount;
//        for (int i = 0; i < yCount + 1; i++) {
//            float value = minY + dy * i;
//            String lab = m2(value);
//            yLabels.put((int) value*100, lab);
//        }
//
//        float tags = (maxY - minY) / KlineIndex.klineIndex;
//        yAxis.setAxisMinimum(minY - tags);
//        yAxis.setAxisMaximum(maxY + tags);

    }

    private static String m2(float f) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(f);
    }

}
